/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Wangyunlai on 2022/5/22.
//

#include "sql/stmt/filter_stmt.h"
#include "common/lang/string.h"
#include "common/log/log.h"
#include "common/sys/rc.h"
#include "storage/db/db.h"
#include "storage/table/table.h"
//#include "sql/parser/expression_binder.h"
//#include "sql/expr/expression.h"
// FilterStmt::~FilterStmt()
// {
  // for (FilterUnit *unit : filter_units_) {
  //   delete unit;
  // }
  // filter_units_.clear();
//}
// TODO: add expr
RC FilterStmt::create(Db *db, Table *default_table, unordered_map<string, Table *> *tables,
    const ConditionSqlNode *conditions, int condition_num, FilterStmt *&stmt, BinderContext binder_context,LogicOp logicop)
{
  RC rc = RC::SUCCESS;
  stmt  = nullptr;

  ExpressionBinder bd(binder_context);
  auto binder = &bd;
  FilterStmt *tmp_stmt = new FilterStmt();
  for (int i = 0; i < condition_num; i++) {
    //FilterUnit *filter_unit = nullptr;

    // rc = create_filter_unit(db, default_table, tables, conditions[i], filter_unit);
    // if (rc != RC::SUCCESS) {
    //   delete tmp_stmt;
    //   LOG_WARN("failed to create filter unit. condition index=%d", i);
    //   return rc;
    // }
    if(conditions[i].expr->type()!= ExprType::COMPARISON) {
      LOG_WARN("condition %d is not a comparison expression", i);
      delete tmp_stmt;
      return RC::INVALID_ARGUMENT;
    }
    if(!binder_context.query_tables().empty()) {
      // 使用 ExpressionBinder 绑定表达式
      unique_ptr<Expression> expr(conditions[i].expr);
      rc = binder->bind_expression(expr, tmp_stmt->filter_units_,db);
      if (rc != RC::SUCCESS) {
        LOG_WARN("failed to bind expression. condition index=%d", i);
        delete tmp_stmt;
        return rc;
      }
    }
    else
    {
      BinderContext context;
      context.add_table(default_table);
      ExpressionBinder expr_binder(context);
      unique_ptr<Expression> expr(conditions[i].expr);
      rc = expr_binder.bind_expression(expr, tmp_stmt->filter_units_,db);
      if (rc != RC::SUCCESS) {
        LOG_WARN("failed to bind expression. condition index=%d", i);
        delete tmp_stmt;
        return rc;
      }
    }
  }
  tmp_stmt->logicop = logicop;
  stmt = tmp_stmt;
  return rc;
}

// RC get_table_and_field(Db *db, Table *default_table, unordered_map<string, Table *> *tables,
//     const RelAttrSqlNode &attr, Table *&table, const FieldMeta *&field)
// {
//   if (common::is_blank(attr.relation_name.c_str())) {
//     table = default_table;
//   } else if (nullptr != tables) {
//     auto iter = tables->find(attr.relation_name);
//     if (iter != tables->end()) {
//       table = iter->second;
//     }
//   } else {
//     table = db->find_table(attr.relation_name.c_str());
//   }
//   if (nullptr == table) {
//     LOG_WARN("No such table: attr.relation_name: %s", attr.relation_name.c_str());
//     return RC::SCHEMA_TABLE_NOT_EXIST;
//   }

//   field = table->table_meta().field(attr.attribute_name.c_str());
//   if (nullptr == field) {
//     LOG_WARN("no such field in table: table %s, field %s", table->name(), attr.attribute_name.c_str());
//     table = nullptr;
//     return RC::SCHEMA_FIELD_NOT_EXIST;
//   }

//   return RC::SUCCESS;
// }

// RC FilterStmt::create_filter_unit(Db *db, Table *default_table, unordered_map<string, Table *> *tables,
//     const ConditionSqlNode &condition, FilterUnit *&filter_unit)
// {
//   RC rc = RC::SUCCESS;

//   CompOp comp = condition.comp;
//   if (comp < EQUAL_TO || comp >= NO_OP) {
//     LOG_WARN("invalid compare operator : %d", comp);
//     return RC::INVALID_ARGUMENT;
//   }

//   filter_unit = new FilterUnit;

  
//     // left_expr
//     unique_ptr<Expression> left_expr(condition.left_expr);
//     if (left_expr == nullptr) {
//       LOG_WARN("left expression is null");
//       return RC::INVALID_ARGUMENT;
//     }
//     FilterObj left_filter_obj;
//     left_filter_obj.init_expr(std::move(left_expr));
//     filter_unit->set_left(left_filter_obj);
  

  
//     // right_expr
//     unique_ptr<Expression> right_expr(condition.right_expr);
//     if (right_expr == nullptr) {
//       LOG_WARN("right expression is null");
//       return RC::INVALID_ARGUMENT;
//     }
//     FilterObj right_filter_obj;
//     right_filter_obj.init_expr(std::move(right_expr));
//     filter_unit->set_right(right_filter_obj);

//     filter_unit->set_comp(comp);

//     // 检查两个类型是否能够比较
//     return rc;
// }
